package cn.qy.kotlin.base

import kotlin.test.Test

class ch06Test {

    // 使用非空断言操作符
    @Test
    fun fun3() {
        var tt = "tt"
        println(tt!!.replaceFirstChar { it.uppercase() })
    }

    // 使用 if 判断 null 值情况
    @Test
    fun fun4() {
        val beverage = null
        if (beverage == null) {
            println("I can't do that without crashing - beverage was null!")
        } else {
            beverage?.capitalize()
        }
        println(beverage)
    }

    // 使用空合并操作符
    @Test
    fun fun5() {
        var beverage = readLine()
        if (beverage == null) {
            println("I can't do that without crashing - beverage was null!")
        } else {
            beverage.capitalize()
        }
        val beverageServed: String = beverage ?: "Buttered Ale"
        println(beverageServed)
    }

    // 空合并操作符也可以和 let 函数一起使用来代替 if/else 语句
    @Test
    fun fun6() {
        var beverage = readLine()
        beverage?.let {
            beverage = it.capitalize()
        } ?: println("I can't do that without crashing - beverage was null!")
    }


}